﻿using Qiniu.Http;
using Qiniu.IO;
using Qiniu.IO.Model;
using Qiniu.RS;
using Qiniu.Util;

namespace Management.API
{
    public class QiniuUtils
    {

        public readonly IConfiguration _configuration;

        public QiniuUtils(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        /// <summary>
        /// 上传文件到七牛云
        /// </summary>
        /// <param name="data">要上传的文件数据</param>
        /// <param name="saveKey">文件在七牛云中的保存名称</param>
        public void UploadFile(byte[] data, string saveKey)
        {
            // 校验输入参数
            if (data == null || data.Length == 0)
            {
                throw new ArgumentException("上传的数据不能为空", nameof(data));
            }

            if (string.IsNullOrWhiteSpace(saveKey))
            {
                throw new ArgumentException("保存的文件名不能为空", nameof(saveKey));
            }

            // 从配置中获取 AccessKey 和 SecretKey
            var accessKey = _configuration["QiNiu:AccessKey"];
            var secretKey = _configuration["QiNiu:SecretKey"];
            if (string.IsNullOrWhiteSpace(accessKey) || string.IsNullOrWhiteSpace(secretKey))
            {
                throw new InvalidOperationException("七牛云的 AccessKey 或 SecretKey 未配置");
            }

            string bucket = "mystoraqe123";


            // 创建上传策略
            PutPolicy putPolicy = new PutPolicy
            {
                Scope = bucket
            };
            // 设置上传凭证有效期（示例2小时，可调整）
            putPolicy.SetExpires(3600);

            // 自动选择区域
            Qiniu.Common.Config.AutoZone(accessKey, bucket, false);

            // 生成上传凭证
            string jstr = putPolicy.ToJsonString();
            string token = Auth.CreateUploadToken(new Mac(accessKey, secretKey), jstr);
            UploadManager um = new UploadManager();

            // 上传数据
            HttpResult result = um.UploadData(data, "images/" + saveKey, token);
            if (result.Code == 200)
            {
                Console.WriteLine("上传成功: " + result.Text);
            }
            else
            {
                Console.WriteLine("上传失败: " + result.Text);
                throw new Exception($"上传失败，状态码: {result.Code}, 错误信息: {result.Text}");
            }
        }

        //删除七牛云上的文件
        public void DeleteFile(string key)
        {
            // 校验输入参数
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("要删除的文件名不能为空", nameof(key));
            }

            // 从配置中获取 AccessKey 和 SecretKey
            var accessKey = _configuration["QiNiu:AccessKey"];
            var secretKey = _configuration["QiNiu:SecretKey"];
            if (string.IsNullOrWhiteSpace(accessKey) || string.IsNullOrWhiteSpace(secretKey))
            {
                throw new InvalidOperationException("七牛云的 AccessKey 或 SecretKey 未配置");
            }

            string bucket = "mystoraqe123";

            // 创建管理对象
            BucketManager bm = new BucketManager(new Mac(accessKey, secretKey));

            // 删除文件
            HttpResult result = bm.Delete(bucket, key);
            if (result.Code == 200)
            {
                Console.WriteLine("删除成功: " + result.Text);
            }
            else
            {
                Console.WriteLine("删除失败: " + result.Text);
                throw new Exception($"删除失败，状态码: {result.Code}, 错误信息: {result.Text}");
            }
        }

    }
}
